<?php
!defined('P_W') && exit('Forbidden');

/**
 *
 * 出售房源服务类
 * @author hu.liaoh
 *
 */
require_once(R_P."mode/house/lib/service/secondbaseservice.php");
class PW_SecondSaleService extends PW_SecondBaseService{
	
	var $roomType = array();//户型枚举
	var $_orderTypes = array();//排序类型
	var $_secondType = 'sale';
	
	function PW_SecondSaleService(){
		$this->roomType = array('1'=>'一室 ', '2'=>'二室 ', '3'=>'三室 ', '4'=>'四室', '5'=>'五室 ', '6'=>'五室以上');
		$this->_orderTypes = array('price', 'unitprice', 'posttime', 'modifytime');
	}

	/**
	 *
	 * 获取一条房源信息
	 * @param unknown_type $houseid
	 */
	function getSaleInfo($houseid){
		$infoDB = $this->_getInfoDB();
		$saleInfo = $infoDB->get($houseid);
		return $this->adapterSecondInfo($saleInfo, 'out');//返回适配后的数据
	}
	
	/**
	 *
	 * 增加一条出售房源信息
	 * @param houseModule $saleModule
	 */
	function addSale($saleModule){
		global  $timestamp;
		if(!$this->_checkSaleModule($saleModule)) return false;
		$saleInfo = $this->_moduleToArray($saleModule);//把module转换成数组		
		if(!S::isArray($saleInfo)) return false;
		$data = $this->adapterSecondInfo($saleInfo,'in');	//对输入数据进行适配		
		$houseid = $this->_addSecondInfo($data);	//增加一条数据到详细信息表中
		if(!$houseid) return false;		
		$this->_addImage($houseid, $data); //增加图片信息表
		$data['houseid'] = $houseid;		
		$this->_addExpired($houseid);
		if(!checkBroker($data['uid'])) $this->_addPersonal($data, 'sale');//添加个人信息到个人信息表	
		$this->_addPosttime($data);	//增加发布时间索引表中的数据
		$this->_addModifyTime($data);	//增加修改时间索引表中的数据
		$this->_addPrice($data);	//增加总售价索引表中的数据
		$this->_addUnitPrice($data);	//增加单价索引表中的数据				
		$this->_addSecondNum($data['uid'], 1); //发布人发布信息数据
		return $houseid;
	}
	
	/**
	 * 
	 *获取经纪人最近更新的房源信息
	 * @param 用户ID $uid
	 * @param 房源ID $houseid
	 * @param 数据条数 $limit
	 */
	function getBorkerSalesByTime($uid, $houseid, $limit){
		$infoDB = $this->_getInfoDB();	//从DB中取出数据
		$result = $infoDB->getBorkerSales($uid, $houseid, $limit);
		return $this->_getDetailInfo($result);
	}
	
	/**
	 * 
	 * 更新一条房源信息
	 * @param $saleModule 房源module
	 * @param $houseids 房源id
	 */
	function updateSale($saleModule, $houseids){
		if(!$this->_checkSaleModule($saleModule)) return false;
		$saleInfo = $this->_moduleToArray($saleModule);//module转换成数组
		if(!S::isArray($saleInfo)) return false;
		$data = $this->adapterSecondInfo($saleInfo,'in');	//对输入数据进行适配
		$this->_updateImage($houseids, $data); //更新图片信息表
		$saleInfo = $this->adapterSecondInfo($data,'in');	//适配过滤输入数据
		$this->_updateSecondInfo($data, $houseids);	//更新详细信息表中的数据
		if(!checkBroker($data['uid'])) $this->_updatePersonal($saleInfo, $houseids);//更新个人信息表
		if($saleInfo['state']) return $this->_updateOverdue($saleInfo, $houseids);//下架|过期房源更新过期信息表并返回
		$this->_updateModityTime($data, $houseids);		//更新修改时间索引表中的数据
		$this->_updatePostTime($data, $houseids);		//更新修改时间索引表中的数据	
		$this->_updatePrice($data, $houseids);		//更新总售价索引表中的数据		
		$this->_updateUnitPrice($data, $houseids);		//更新单价索引中的数据
		return true;
	}
	
	/**
	 *
	 * 删除一条多条房源信息
	 * @param $houseids
	 */
	function delete($houseids){
		if(!S::isArray($houseids)) $houseids = array($houseids);
		$this->_updateSecondNums($houseids);   //更新用户发布数量
		$this->_deleteSecondInfo($houseids);   //删除详细信息表中的数据
		$this->_deleteModifyTime($houseids);  //删除修改时间索引表中的数据
		$this->_deletePostTime($houseids);	 //删除发布时间索引表中的数据
		$this->_deletePrice($houseids);		 //删除总售价索引表中的数据
		$this->_deleteUnitPrice($houseids);	//删除单价索引中的数据
		$this->_deleteExpired($houseids);  //删除过期数据表
		$this->_deleteImage($houseids);   //删除图片表
		$this->_deleteOverdue($houseids);  //删除过期索引表
		$this->_deletePersonal($houseids); //删除个人信息表数据
		return true;
	}
	
	/**
	 * 
	 * 获获出售module
	 * @param $houseid
	 */
	function getHouseSaleModule($houseid){
		if(!$houseid) return $this->_getSaleModule();
		$saleModule = $this->_getSaleModule();
		$infoDB = $this->_getInfoDB();
		$saleInfo = $infoDB->get($houseid);
		return $this->_arrayToModule($saleInfo, $saleModule);
	}
	
	/**
	 * 
	 * 获取数据map
	 */
	function _getSecondMap(){
		return array('attachs'=>'attachs', 'tel'=>'tel', 'email'=>'email', 'phone'=>'phone', 'linkman'=>'linkman', 'areatype'=>'areatype', 'housetype'=>'housetype', 'subject'=>'subject', 'uid'=>'uid', 'username'=>'username', 'istop'=>'istop', 'isurgent'=>'isurgent', 'isbroker'=>'isbroker','acreage'=>'acreage','currentfloor'=>'currentfloor','totalfloor'=>'totalfloor','price'=>'price','unitprice'=>'unitprice','room'=>'room','righter'=>'righter','village'=>'village','platetype'=>'plate','address'=>'address','buildtime'=>'buildtime','traffic'=>'traffic','fitment'=>'fitment','equipment'=>'equipment','thumb'=>'thumb','state'=>'state','descrip'=>'descrip','gate'=>'gate',	'posttime'=>'posttime',	'modifytime'=>'modifytime');
	}
	
	/**
	 *
	 * 增加单价索引表中的数据
	 * @param $saleInfo房源信息
	 */
	function _addUnitPrice($saleInfo){
		$unitPriceDB = $this->_getUnitPriceDB();
		$unitPriceData = $this->_filterField($saleInfo, $unitPriceDB->getStruct('add'));
		$retV = $unitPriceDB->add($unitPriceData);
		if(!S::isnum($retV))return false;
		return $retV;
	}

	/**
	 * 
	 * 增加经纪人发布信息数量
	 * @param $uid
	 * @param $num
	 */
	function _addSecondNum($uid,$num){
		$brokerService = $this->_getBrokerService();
		$brokerService->addSaleNum($uid,$num);
	}
	
	/**
	 * 
	 * 重新发布房源
	 * @param $houseid
	 */
	function _overdueToNomal($houseids){
		global $timestamp;
		$overdueDB = $this->_getOverdueDB();
		$result = $overdueDB->gets($houseids);
		foreach($result as $key=>$value){
			$value['posttime'] = $value['modifytime'] = $timestamp;
			$this->_addModifyTime($value);
			$this->_addPostTime($value);
			$this->_addUnitPrice($value);
			$this->_addPrice($value);
		}
		$overdueDB->delete($houseids);
		return true;
	}
	
	/**
	 * 
	 * 移动过期房源到过期房索引表中
	 * @param $houseids
	 */
	function _nomalToOverdue($houseids){	
		$infoDB = $this->_getInfoDB();
		$overdueDB = $this->_getOverdueDB();
		$result = $infoDB->getIndexInfo($houseids);
		foreach ($result as $key=>$value){
			$data = $this->_filterField($value, $overdueDB->getStruct());
			$overdueDB->add($data);
		}
		$this->_deleteModifyTime($houseids);
		$this->_deletePostTime($houseids);
		$this->_deletePrice($houseids);
		$this->_deleteUnitPrice($houseids);
		return true;
	}
	
	/**
	 * 
	 * 更新过期索引表
	 * @param $saleInfo
	 * @param $houseids
	 */
	function _updateOverdue($saleInfo, $houseids){
		$overdueDB = $this->_getOverdueDB();
		$overdueData = $this->_filterField($saleInfo,$overdueDB->getStruct('update'));
		$retV = $overdueDB->update($overdueData, $houseids);
		if($retV == false) return false;
	}

	/**
	 *
	 * 更新单价索引中的数据
	 * @param $saleInfo房源信息
	 */
	function _updateUnitPrice($saleInfo, $houseids){
		$unitPriceDB = $this->_getUnitPriceDB();
		$unitPriceData = $this->_filterField($saleInfo,$unitPriceDB->getStruct('update'));
		$retV = $unitPriceDB->update($unitPriceData, $houseids);
		if($retV == false) return false;
	}

	/**
	 *
	 * 删除单价索引中的数据
	 * @param $houseids房源IDS
	 */
	function _deleteUnitPrice($houseids){
		$unitPriceDB = $this->_getUnitPriceDB();
		$retV = $unitPriceDB->delete($houseids);
		if($retV == false) return false;
	}

	/**
	 *
	 * 对输入数据内容进行过安全过滤及枚举转换
	 * @param $saleInfo
	 */
	function _checkInput($saleInfo){
		global $timestamp;
		$saleInfo['datatype'] = 'sale';
		if($saleInfo['isbroker']){
			$borkerService = $this->_getBrokerService();
			$borker = $borkerService->getBroker($saleInfo['uid']);
			$saleInfo['linkman'] = $borker['realname'];
		}
		$saleInfo['posttime'] = $saleInfo['modifytime'] = $timestamp;
		$saleInfo['room'] = $saleInfo['room']['bedroom'] . $saleInfo['room']['hall'] . $saleInfo['room']['toilet'] . $saleInfo['room']['terrace'];
		$saleInfo['unitprice'] = ($saleInfo['acreage'] != 0) ? ceil(($saleInfo['price'] * 10000 / $saleInfo['acreage'])) : 0 ;		//计算单价
		$saleInfo['gate'] = $saleInfo['subject'].$saleInfo['village'];
		$saleInfo['roomtype'] = (int)$saleInfo['room']['bedroom'];	//计算房型
		if($saleInfo['roomtype'] > 5) $saleInfo['roomtype'] = 6;		
		$saleInfo['acreagetype'] = $this->_getIdFromRange('acreage',$saleInfo['acreage']);		//计算面积区间
		$saleInfo['pricetype'] = $this->_getIdFromRange('sellprice',$saleInfo['price']);	//计算售价区间
		return $saleInfo;
	}

	/**
	 *
	 * 对输出数据内容进行枚举转换
	 * @param $saleInfo
	 */
	function _checkOutput($saleInfo){
		$room = str_split($saleInfo['room']);
		$saleInfo['room'] = $room[0].'室'.$room[1].'厅'.$room[2].'卫'.$room[3].'阳台';	//户型转换
		$areaFields = $this->getFieldsByType('area');		//区域转换
		$saleInfo['areatype'] = $areaFields{$saleInfo['areatype']};
		if($saleInfo['fitment']){
			$fitmentFields = $this->getFieldsByType('paystatus');
			$saleInfo['fitment'] = $fitmentFields[$saleInfo['fitment']];
		}
		$thumb = geturl($saleInfo['thumb']);
		$saleInfo['thumb'] = $thumb[0] ;
		if(!is_file($saleInfo['thumb'])) $saleInfo['thumb'] = 'mode/house/images/sendhand/nopic_105x80.png';		
		$saleInfo['neartime'] =  getLastDate($saleInfo['modifytime']);
		if(!$saleInfo['isbroker'])	$saleInfo['linkman'] = '个人 ';
		
		$acreagetype = $this->_getIdFromRange('acreage',$saleInfo['acreage']);		//计算面积区间
		$pricetype = $this->_getIdFromRange('sellprice',$saleInfo['price']);	//计算售价区间

		($acreagetype != $saleInfo['acreagetype']) && $data['acreagetype'] = $acreagetype;
		($pricetype != $saleInfo['pricetype']) && $data['pricetype'] = $pricetype;
		if(is_array($data)){
			$this->_updateModityTime($data, $saleInfo['houseid']);
			$this->_updatePostTime($data, $saleInfo['houseid']);	
			$this->_updatePrice($data, $saleInfo['houseid']);		
			$this->_updateUnitPrice($data, $saleInfo['houseid']);
		}
		$topSale = $GLOBALS['house_sale'][$saleInfo['houseid']];
		$saleInfo['istop'] && $topSale && ($saleInfo['pricetype'] !=  $topSale['pricetype'] || $saleInfo['acreagetype'] !=  $topSale['acreagetype']) && $this->_updateConfig('sale');
		return $saleInfo;
	}

	/**
	 * 
	 * 检查module是否合法
	 * @param $saleModule
	 */
	function _checkSaleModule($saleModule) {
		return is_object($saleModule) && get_class($saleModule) == 'PW_HouseSaleModule';
	}
	
	/**
	 * 
	 * 获取个人信息服务
	 */
	function _getPersonalDB(){
		return House::LoadDB('House_Personal');
	}
	
	/**
	 * 
	 * 获取过期索引表DB
	 */
	function _getOverdueDB(){
		return House::LoadDB('House_Sale_Overdue');
	}

	/**
	 *
	 * 获取db类
	 * @param $name 工具类名称
	 */
	function _getSecondDB(){
		return House::LoadDB('House_SecondSale');
	}

	/**
	 *
	 * 获取出售房module
	 */
	function _getSaleModule() {
		return House::loadModule('HouseSaleModule');
	}

	/**
	 *
	 *获取信息表db类
	 */
	function _getInfoDB(){
		return House::LoadDB('House_Sale_Info');
	}

	/**
	 *
	 * 获取发布时间db类
	 */
	function _getPosttimeDB(){
		return House::LoadDB('House_Sale_Posttime');
	}

	/**
	 *
	 * 获取修改时间db类
	 */
	function _getModifytimeDB(){
		return House::LoadDB('House_Sale_Modifytime');
	}
	
	/**
	 *
	 * 获取售价db类
	 */
	function _getPriceDB(){
		return House::LoadDB('House_Sale_Price');
	}

	/**
	 *
	 * 获取单价db类
	 */
	function _getUnitPriceDB(){
		return House::LoadDB('House_Sale_Unitprice');
	}
}
